//
//  VideoCell.swift
//  douyinSwift
//
//  Created by 于波 on 2019/11/15.
//  Copyright © 2019 于波. All rights reserved.
//

import UIKit
import MarqueeLabel
import Kingfisher
import ChainableAnimations

class VideoCell: UITableViewCell {
    
    // 头像下面 关注按钮 旋转动画
    var animator1 : ChainableAnimator!
    
    // 唱片动画
    var animator2 : ChainableAnimator!
    
    // cell将要复用的时候 执行什么
    override func prepareForReuse() {
        //重置关注按钮的所有动画状态
        if animator1 != nil {
            animator1.stop()
            //transform 的状态恢复到原始状态 identity
            addFollowBtn.transform = .identity
            // 移除所有layers上的动画
            addFollowBtn.layer.removeAllAnimations()
            addFollowBtn.setImage(UIImage(named: "icon_personal_add_little"), for: .normal)
        }
        
        if animator2 != nil {
            animator2.stop()
            diskSubView.transform = .identity
            diskSubView.layer.removeAllAnimations()
        }
        
        diskView.resetViewAnimation()
    }
    
    @IBAction func addFollowTap(_ sender: UIButton) {
        print("点击了 关注")
        
        animator1 = ChainableAnimator(view: sender)
        
        // transitionCrossDissolve 关注按钮 切换图片
        UIView.transition(with: sender, duration: 0.2, options: UIView.AnimationOptions.transitionCrossDissolve, animations: {
            sender.setImage(UIImage(named: "iconSignDone"), for: .normal)
        }) { (_) in
            self.animator1.rotate(angle: 360).thenAfter(t: 0.5).wait(t: 0.3).transform(scale: 0).animate(t: 0.2)
        }
    }
    
    var aweme : AwemeList! {
        didSet {
            authorLabel.text = aweme.author?.nickname
            videoDescLabel.text = aweme.desc
            
            //封面第一帧图片  !解析一定有值
            let cover = aweme.video!.cover!.urlList![0]
            let url = URL(string: cover)
            coverImageView.kf.setImage(with: url)
            
            //设置音乐标题和作者
            musicLabel.text = aweme.music!.title! + "-" + aweme.music!.author!
            
            //点赞 评论  转发
            // description int类型转txt
            likeNumLabel.text = aweme.statistics!.diggCount!.toWanStr
            commentNumLabel.text = aweme.statistics!.commentCount!.toWanStr
            shareNumLabel.text = aweme.statistics!.shareCount!.toWanStr
            
            //作者头像
            let testPicUrlString = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1574414501147&di=3e72d9b2b7713df4e3bc20cb0221aa9f&imgtype=0&src=http%3A%2F%2Fpic4.zhimg.com%2F50%2Fv2-0019ec92840b3cda9c12445d4452e4a5_hd.jpg"
            let authorAvatar = aweme.author!.avatarThumb!.urlList![0]
            let authorUrl = URL(string: testPicUrlString)!
            followBtn.kf.setImage(with: authorUrl, for: .normal)
            // 通过代码在xib中实现
//            followBtn.layer.cornerRadius = 25
//            followBtn.clipsToBounds = true
            
            //唱盘音乐封面
            let testMusicUrlString = "http://i0.hdslb.com/bfs/article/4380ff8dad9c01f0385fa8e7372ff9488ebb6227.jpg"
            let musicCover = aweme.music!.coverThumb!.urlList![0]
            musicCoverImageView.kf.setImage(with: URL(string: testMusicUrlString))
            
            //唱片旋转动画
            animator2 = ChainableAnimator(view: diskSubView)
            //rotate旋转180度 animateWithRepeat重复动画 3.5s一次 重复50次
            animator2.rotate(angle: 180).animateWithRepeat(t: 3.5, count: 50)
            
            //唱片周围 音符散发动画
            diskView.raiseAnimate(imgName: "icon_home_musicnote1", delay: 0)
            diskView.raiseAnimate(imgName: "icon_home_musicnote2", delay: 1)
            diskView.raiseAnimate(imgName: "icon_home_musicnote1", delay: 2)
        }
    }
    
    //主封面
    @IBOutlet weak var coverImageView: UIImageView!
    @IBOutlet weak var pauseImageView: UIImageView!
    
    //作者
    @IBOutlet weak var authorLabel: UILabel!
    @IBOutlet weak var videoDescLabel: UILabel!
    @IBOutlet weak var musicLabel: MarqueeLabel!
    
    //关注
    @IBOutlet weak var followBtn: UIButton!
    @IBOutlet weak var addFollowBtn: UIButton!
    
    //喜欢
    @IBOutlet weak var likeBtn: UIButton!
    @IBOutlet weak var likeNumLabel: UILabel!
    
    //评论
    @IBOutlet weak var commentBtn: UIButton!
    @IBOutlet weak var commentNumLabel: UILabel!
    
    //转发
    @IBOutlet weak var shareBtn: UIButton!
    @IBOutlet weak var shareNumLabel: UILabel!
    
    //唱片
    @IBOutlet weak var diskView: UIView!
    @IBOutlet weak var diskSubView: UIView!
    //唱片封面
    @IBOutlet weak var musicCoverImageView: UIImageView!
    @IBOutlet weak var rotateDiskView: UIImageView!
    
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
